<?php
//开启session
session_start();
//包含数据库配置文件
include '../public/dbconfig.php';
//包含函数库文件
include '../public/functions.php';
//调用函数 链接数据库
$link = con();

//接收帖子id
$tid = $_GET['tid'];
//接收分页
$page = $_GET['page'];
//接收发帖人的id
$uid = $_GET['uid'];
//接收版块id
$cid = $_GET['cid'];
//接收现有人名币
$umoney = $_GET['umoney'];
//接收此贴需要购买的人名币
$gmmoney = $_GET['gmmoney'];

//判断用户是否有钱购买此贴
if($umoney < $gmmoney){
	echo '<script>alert("人名币不够哦,快去发帖挣人名币吧");window.location.href="list.php?cid='.$cid.'&page='.$page.'"</script>';
	exit;
}
//准备购买语句
$gmsql = "update user set money=({$umoney} - {$gmmoney}) where id={$_SESSION['id']}";

//执行购买语句
$gmres = mysqli_query($link, $gmsql);
//判断执行结果
if($gmres){
	//查询发帖人的人名币
	$ufsql = "select money from user where id={$uid}";
	//执行sql
	$ufres = mysqli_query($link, $ufsql);
	//赋值 发帖人现有人名币
	$ufmoney = mysqli_fetch_assoc($ufres);
	//发帖用户加人名币
	$usql = "update user set money=({$ufmoney['money']} + {$gmmoney}) where id={$uid}";
	//执行sql
	$ures = mysqli_query($link, $usql);
	
	//向表里添加数据
	$gmsql = "insert into isgoumai(uid,fid)values('{$_SESSION['id']}','{$tid}')";
	//执行sql
	$gmres = mysqli_query($link, $gmsql);
	echo '<script>alert("购买成功");window.location.href="list.php?cid='.$cid.'&page='.$page.'"</script>';
}else{
	echo '<script>alert("购买失败");window.location.href="list.php?cid='.$cid.'&page='.$page.'"</script>';
}
//关闭数据库链接
mysqli_close($link);